package com.se.optionView;

import com.se.common.ConnectionType;
import com.se.common.Message;
import com.se.common.MessageType;
import com.se.utils.ClientUtils;
import com.se.utils.ServiceUtils;
import com.se.utils.StageManager;
import com.se.utils.StageType;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicReference;

public class JoinGroupController implements Initializable {
    @FXML
    public TextField tfGroupId;

    @FXML
    public Button btnJoinGroup;

    @FXML
    public AnchorPane utilsPane;

    @FXML
    public Button btnClose;

    @FXML
    public Button btnCancel;

    private static JoinGroupController instance;

    public static JoinGroupController getInstance() {
        return instance;
    }

    public JoinGroupController() {
        super();
        instance = this;
    }

    @FXML
    public void btnJoinGroupOnClick() {
        String groupId = tfGroupId.getText();

        if(groupId == null || groupId.isEmpty()) {
            ClientUtils.information_Alert("群组ID不能为空");
            return;
        }

        if(!ServiceUtils.isInteger(groupId)) {
            ClientUtils.information_Alert("群组ID错误！");
            return;
        }

        Message message = new Message();
        message.setMsgType(MessageType.MESSAGE_JOIN_GROUP);
        message.setContent(groupId);
        message.setSendUser(ServiceUtils.getSpeaker().getUserName());
        try {
            ClientUtils.sendMessage(ServiceUtils
                    .getConnection(ConnectionType.BASE_CONNECTION).getSocket(), message);
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    @FXML
    public void btnCancelOnClick() {
        StageManager.get(StageType.JOIN_GROUP_STAGE).close();
        StageManager.remove(StageType.JOIN_GROUP_STAGE);
    }

    @FXML
    public void btnCloseOnClick() {
        StageManager.get(StageType.JOIN_GROUP_STAGE).close();
        StageManager.remove(StageType.JOIN_GROUP_STAGE);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        btnClose.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/com/se/css/close-button.css")).toExternalForm());

        btnJoinGroup.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/com/se/css/send-button.css")).toExternalForm());

        btnCancel.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/com/se/css/send-button.css")).toExternalForm());

        AtomicReference<Double> offsetX = new AtomicReference<>((double) 0);
        AtomicReference<Double> offsetY = new AtomicReference<>((double) 0);
        utilsPane.setOnMousePressed(event -> {
            offsetX.set(event.getSceneX());
            offsetY.set(event.getSceneY());
        });

        utilsPane.setOnMouseDragged(event -> {
            Stage stage = StageManager.get(StageType.JOIN_GROUP_STAGE);
            stage.setX(event.getScreenX() - offsetX.get());
            stage.setY(event.getScreenY() - offsetY.get());
        });
    }
}
